<template>
<!-- 微信群机器人 -->
  <!-- 搜索工作栏 -->
  <div v-show="setView==='list'">
      <Search :schema="allSchemas.searchSchema" @search="setSearchParams" :show-reset="false"  @reset="setSearchParams">

      </Search>
<div style="margin: 10px 0;">
  <el-button
            type="primary"
            @click="openForm('create','')"
            v-hasPermi="['robot:save']"
          >
            新增
          </el-button>
</div>
    <!-- 列表 -->
      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="loading"
        :pagination="{
          total: tableObject.total
        }"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
         :stripe="true"
        header-cell-class-name="custom-header"
      >
      <template #name="{ row }">
        <el-button
            link
            type="primary"
          @click="openDetail(row.id)"
          >
          {{ row.name }}
          </el-button>
        </template>
      <template #isTest="{ row }">
          <span>{{ row.isTest !=1?'是':'否' }}</span>
        </template>
      <template #isUse="{ row }">
          <el-switch
            v-model="row.isUse"
            active-value="0"
            inactive-value="1"
            inline-prompt
            :active-icon="Check" :inactive-icon="Close"
            @change="handleStatusChange(row)"
            :disabled="!checkPermissions(['robot:isValid'])"
          />
        </template>
        <template #action="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            :disabled="row.isUse=='0'"
             v-hasPermi="['robot:update']"
          >
            编辑
          </el-button>
          <!-- <el-button
            link
            type="primary"
            @click="openDetail(row.id)"
          >
            详情
          </el-button> -->
          <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            :disabled="row.isUse=='0'"
            v-hasPermi="['robot:delete']"
          >
            删除
          </el-button>
        </template>
      </Table>
    </div>
  <ContentWrap v-show="setView==='add'">
    <!-- 表单弹窗：添加/修改 -->
    <GroupRobotsForm ref="formRef" @success="getList" @setClose=setClose />
  </ContentWrap>

  <ContentWrap v-show="setView==='view'">
    <!-- 详情弹窗 -->
    <GroupRobotsDetail ref="detailRef" @setClose=setClose />
  </ContentWrap>

</template>
<script lang="ts" setup>
import { allSchemas } from './groupRobots.data';
import * as weComNewsApi from '@/api/news/weComNews';
import GroupRobotsForm from './GroupRobotsForm.vue';
import GroupRobotsDetail from './GroupRobotsDetail.vue';
import { Check, Close } from '@element-plus/icons-vue'

defineOptions({ name: 'GroupRobots' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject, tableMethods } = useTable(
  {
    getListApi: weComNewsApi.getRobotPage, // 分页接口
    delListApi: weComNewsApi.deleteRobot, // 删除接口
    defaultParams:{type:0} //传递默认数据
  }
)
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods
const emit = defineEmits(['setViewFun']) 

const loading = ref(false)
const setView = ref('list');
const setViewFun=(type:string)=>{
  setView.value=type;
}
const setClose=()=>{
  setView.value='list';
}
const tableData=[
  {
    name:'机器人1',
    webHookUrl:'https://qyapi.weixin.qq.com/cgibin/webhook/send?key=c73daa86-3321-4731-a233-1abfd16f2839',
    isGroup:true,
    status:true
  }
]
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number|string) => {
  // formRef.value.open(type, id)
  // setViewFun('add')
  emit('setViewFun',{type, id,str:'GroupRobotsadd'});

}

/** 详情操作 */
const detailRef = ref()
const openDetail = (id: number|string) => {
  detailRef.value.open(id)
  setViewFun('view')
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
    // 批量删除 获取id数组
    try {
      //确认删除
      await message.delConfirm()
      await weComNewsApi.deleteRobot([id])
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  }
// const handleDelete = (id) => {
  
//   tableMethods.delList([id], false)
// }
/** 修改状态 */
const handleStatusChange = async (row: any) => {
  console.log(row,'000000',row.isUse);
  await weComNewsApi.updateValid({id:row.id,isValid:row.isUse})
}
defineExpose({ getList }) 

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style lang="scss" scoped>



</style>